package com.hzd.web;

import ablaze.framework.api.result.ExecuteResult;
import ablaze.framework.config.SystemConfig;
import ablaze.framework.page.PageInfo;
import ablaze.framework.web.DataController;
import com.hzd.condition.TenantCondition;
import com.hzd.entity.Tenant;
import com.hzd.service.RentService;
import com.hzd.service.WxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author Kent
 * @date 2015/8/17.
 */
@Controller
@RequestMapping(value = "/rent")
public class RentController extends DataController {

    //微信域名
    private static final String DOMAIN_NAME = SystemConfig.getString("wx.domain.name");

    private static final String SHARE_IMG_URL = DOMAIN_NAME + "/rent/static/img/activity/rent_share_img.jpg";


    @Autowired
    private RentService rentService;

    @Autowired
    private WxService wxService;

    private static final Logger LOGGER = LoggerFactory.getLogger(RentController.class);

    //------------------------------------PC端调用----------------------------------------
    @RequestMapping(value = "/list")
    public String tenantList() {
        return "rent/TenantList";
    }

    @RequestMapping(value = "/findAll")
    @ResponseBody
    public List<Tenant> findAll(TenantCondition condition) {

        return rentService.findAll(condition);
    }


    @RequestMapping(value = "/findPagedList")
    @ResponseBody
    public PageInfo findPagedList(Integer page,Integer rows,TenantCondition condition){

        return rentService.findApplyPagedList(page,rows,condition);
    }


    //------------------------------------手机端调用----------------------------------------

    /**
     * 租+申请微信授权回调接口
     *
     * @param code
     * @param state
     * @param attributes
     */
    @RequestMapping(value = "/applyOAthCallBack")
    public String applyOAthCallBack(String code, String state, RedirectAttributes attributes) {

        if (isEmpty(code)) {
            LOGGER.info("微信授权回调未返回code");
        }


        attributes.addFlashAttribute("openId", rentService.getOpenId(code));

        //从活动页面跳转过来
        if ("fromAcivityFlag".equals(state)) {
            attributes.addFlashAttribute("fromAcivityFlag", true);
        } else {
            attributes.addFlashAttribute("fromAcivityFlag", false);
        }

        return "redirect:/rent/apply";
    }

    /**
     * 跳转到申请页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/apply")
    public String apply(Model model) {
        return "rent/Tenant";
    }


    @RequestMapping(value = "/orgApplyOAthCallBack")
    public String orgApplyOAthCallBack(String code,String state) {
        if (isEmpty(code)) {
            LOGGER.info("微信授权回调未返回code");
        }

        String openid = rentService.getOpenId(code);

        return "redirect:/rent/orgApplyPage?openid=" + openid;
    }

    @RequestMapping(value = "/orgApplyPage")
    public String orgApplyPage(String openid,Model model){

        model.addAttribute("openid",openid)
                .addAttribute("config",wxService.getJsConfig(wxService.getCurrentURL(getRequest())))
                .addAttribute("shareImgUrl",SHARE_IMG_URL)
                .addAttribute("shareUrl",wxService.getShareUrl(DOMAIN_NAME + "/rent/rent/orgApplyOAthCallBack",null));

        return "rent/OrgApply";
    }


    /**
     * 保存
     *
     * @param tenant
     * @return
     */
    @RequestMapping(value = "/store")
    @ResponseBody
    public ExecuteResult store(Tenant tenant) {

        return rentService.saveOrUpdate(tenant);
    }

    /**
     * 发送手机验证码
     *
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/sendVerifyCode")
    @ResponseBody
    public ExecuteResult sendVerifyCode(String mobile) {

        return rentService.sendVerifyCode(mobile);
    }

    @RequestMapping(value = "/applySuccess")
    public String applySuccess(String id,Model model){

        model.addAttribute("result",rentService.operRentAmount(id));

        return "/rent/ApplySuccess";
    }
}
